import 'package:toatool/src/data/remote/app_exceptions.dart';

import 'view_state_controller.dart';

/// 基于ViewStateController
abstract class ViewStateDetailController<T> extends ViewStateController {
  late dynamic queryData;

  /// 页面数据
  late T detail;

  // 刷新
  Future<void> mitRefresh() async {
    try {
      var data = await loadData(queryData);
      if (data == null) {
        setEmpty();
      } else {
        onCompleted(data);
        detail = data;
        setIdle();
      }
      update();
    } on AppException catch (e, s) {
      setError(e, s, message: e.message);
      update();
    } catch (e, s) {
      setError(e, s, message: '出错了');
      update();
    }
  }

  // 加载
  Future<T> loadData([dynamic queryData]);

  // 完成
  void onCompleted(T data) {}
}
